<?php

include_once 'staff.php';
include_once 'roster.php';
include_once './libs/config.php';
/*
 * 1.inital roster,(finished)
 * 2.prepare the StartdutiesArray(finished)
 * 3.update roster (swop person)
 * 4.update duty name
 * 5.printout finial roster diagram(date/duty/people)
 */

/**
 * Description of admin
 *
 * @author markxi
 */
class admin extends staff {

    //private $staffArray = array();
    private $roster;
    //caculating fucntion variables
    private $DutiesPoolArray = array();
    protected $DBconfig;

    /* $dutiesPoolArray[]={
     *      [Duty1]=>array('mk1','mk2','mk3')
     *      [Duty2]=>array('mk2','mk3')
     * } 
     */

    //end of cacualting function variables

    /*
     * fist time to run the program
     */
    public function initialRosterTable($TempRosterArray, $date1, $date2) {
        $dbName = parent::getDBName();
        $sqlDuties1 = "DROP TABLE IF EXISTS `$dbName`.`duties`;";
        
        $this->DBconfig->DB->query($sqlDuties1);
        $sqlDuties2 ="CREATE TABLE `$dbName`.`duties` (`duty_id` int(10) NOT NULL AUTO_INCREMENT,`dutyName` varchar(50) NOT NULL,`description` varchar(500) NOT NULL,PRIMARY KEY (`duty_id`)) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
        $this->DBconfig->DB->query($sqlDuties2);
        
        foreach ($TempRosterArray as $value) {
            $tempInsertDuties = "INSERT INTO `$dbName`.`duties` (`duty_id`, `dutyName`, `description`) VALUES (NULL, '$value','');";
            $this->DBconfig->DB->query($tempInsertDuties);
        }
        
        $this->setRosterDutiesArray($TempRosterArray);//after admin first time setup duties array, create roster table
        $this->initalRoster($date1, $date2);
        $TempSundaysArray = $this->roster->getSundaysArray();
        $TempCount = count($TempRosterArray);
        $TempString ='';
        for($i = 0; $i<$TempCount;$i++){
            $TempString=$TempString."`$TempRosterArray[$i]` varchar(30) NOT NULL, ";
        }
        //echo '<br>'.$TempString.'<br>';
        $sql1 = "DROP TABLE IF EXISTS `$dbName`.`roster`;";
        $sql2 = "CREATE TABLE `$dbName`.`roster` (`roster_id` int(10) NOT NULL,`Date` date NOT NULL,$TempString PRIMARY KEY (`roster_id`,`Date`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
   
        $this->DBconfig->DB->query($sql1);
        $this->DBconfig->DB->query($sql2);
        
        //--start insert earch 
        //fist identify how many duties are included.
        //prepare the duties list string
        $TempDutiesString = '';
        $TempDutiesInsertEmptyString ='';
        for($i = 0; $i<$TempCount;$i++){
            $TempDutiesString=$TempDutiesString.",`$TempRosterArray[$i]`";
            $TempDutiesInsertEmptyString=$TempDutiesInsertEmptyString.",''";
        }
        $TempSundaysArray1=$this->roster->getSundaysArray();
        foreach ($TempSundaysArray1 as $value) {
            $sqlSundayInsert = "INSERT INTO `$dbName`.`roster` (`roster_id`, `Date` $TempDutiesString) VALUES (1, '$value'$TempDutiesInsertEmptyString);";
            $this->DBconfig->DB->query($sqlSundayInsert);
        }
        
    }

    /*
     * end of fist time
     */
    
    
    public function initial($Name, $PW){
        $sqlTempUser1 = "DROP TABLE IF EXISTS `user`;";
        $this->DBconfig->DB->query($sqlTempUser1);
        $sqlTempUser2 ="CREATE TABLE `duties` (`id` int(10) NOT NULL AUTO_INCREMENT,`dutyName` varchar(50) NOT NULL,`description` varchar(500) NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
        $this->DBconfig->DB->query($sqlTempUser2); 
    }

    public function __construct($Email, $PW) {
        parent::__construct($Email, $PW);
    }
    
    
    public function updateRosterDBToUserDB($newDB){
        $id = parent::getID();
        $TempUpdateRosterDBToUserDB = "UPDATE  `roster_users`.`users` SET  `rosterDBName` =  '$newDB' WHERE  `users`.`user_id` =$id";
        $this->DBconfig->DB->query($TempUpdateRosterDBToUserDB);
        parent::setDBName($newDB);
    }
    

    public function caculateSundays() {
        $this->roster->caculateSundayArray();
    }

    public function getSundayArray() {
        $this->roster->getSundayArray();
    }

    public function displayStaff() {
        //DB->getStaff();
    }

    public function updateStaff() {
        
    }

    public function initalRoster($date1, $date2) {
        $this->roster = new roster($date1, $date2);
    }

    public function setRosterDutiesArray($DutiesArray) {
        //$this->roster->setDuitesArray($DutiesArray);
        foreach ($DutiesArray as $key => $value) {

            $sql1 = "INSERT INTO `roster`.`duties` (`id`, `dutyName`, `description`) VALUES (NULL, '$value', '')";
            $this->DBconfig->DB->query($sql1);
            //echo "$sql<br>";
        }
        // ;
    }

    //get all the roster duties from the db and then reset into the Array
    public function getRosterDutiesArray() {
        $sqlTemp = "SELECT `dutyName` FROM `duties`";
        $this->DBconfig->DB->query($sqlTemp);
        $result = $this->DBconfig->DB->get();
        $tempArray = array();
        foreach ($result as $value) {
            $tempArray[] = $value['dutyName'];
        }
        $this->roster->setDuitesArray($tempArray);
        return $this->roster->getDutyArray();
    }

    //call getRosterDutiesArray() above to get 
    public function showRosterDutiesArray() {
        $this->getRosterDutiesArray();
        $this->roster->showDutyArray();
    }

    public function checkpw($Email, $pw) {
        parent::checkPW($Email, $pw);
    }

}

?>
